import pygame
import random

SCREEN_SIZE = (480, 700)


class GameSprite(pygame.sprite.Sprite):
    def __init__(self, file_name, speed=1):
        super().__init__()
        self.image = pygame.image.load(file_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y += self.speed


class BackGroundSprte(GameSprite):
    def __init__(self, is_alt=False):
        super().__init__('./image/background.png')
        if is_alt:
            self.rect.y = - SCREEN_SIZE[1]

    def update(self):
        super().update()
        if self.rect.y>=SCREEN_SIZE[1]:
            self.rect.y = -SCREEN_SIZE[1]
        self.rect.centery += 5

class Plane(GameSprite):
    def __init__(self):
        super().__init__('./image/me1.png')
        self.rect.centery = 600
        self.rect.centerx = 240
        self.bullet_group = pygame.sprite.Group()
    def fire(self):
        bullet= Bullet()
        bullet.rect.x = self.rect.centerx
        self.bullet_group.add(bullet)

class Enemy_Airplane(GameSprite):
    def __init__(self):
        super().__init__('./image/enemy1.png')
        self.rect.centery = 50
        self.rect.centerx = random.randint(30,450-self.rect.w)

    def update(self):
        self.rect.y+=self.speed
        if self.rect.y>SCREEN_SIZE[1]:
            self.kill()
class Bullet(GameSprite):
    def __init__(self):
        super().__init__('./image/bullet1.png')
        self.rect.centery = 600
        self.rect.centerx = 240
    def update(self):
        self.rect.y -=self.speed
        if self.rect.y<0:
            self.kill()
class PlaneGame:
    def __init__(self):
        """初始化"""
        pygame.init()
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption("飞机大战")
        self.clock = pygame.time.Clock()
        pygame.time.set_timer(pygame.USEREVENT, 5000)
        self.is_loop = True
        self._create_sprite()

    def start_game(self):
        """开始游戏"""
        while self.is_loop:
            self._event_handler()
            self.clock.tick(80)
            self.update_sprite()
            pygame.display.update()
        pygame.quit()

    def _create_sprite(self):
        #
        bg1 = BackGroundSprte()
        bg2 = BackGroundSprte(True)
        self.bg_group = pygame.sprite.Group(bg1, bg2)
        self.plane = Plane()
        self.plane_group = pygame.sprite.Group(self.plane)
        self.Enemy_Airplane = Enemy_Airplane()
        self.Enemy_Airplane_group = pygame.sprite.Group(self.Enemy_Airplane)
    def update_sprite(self):
        """更新精灵"""
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        self.plane_group.draw(self.screen)
        self.plane.bullet_group.update()
        self.plane.bullet_group.draw(self.screen)
        self.Enemy_Airplane_group.update()
        self.Enemy_Airplane_group.draw(self.screen)
        pygame.sprite.groupcollide(self.plane.bullet_group, self.Enemy_Airplane_group, True, True)
        spirtes = pygame.sprite.spritecollide(self.plane, self.Enemy_Airplane_group,False)
        if len(spirtes)>0:
            print('Game OVER')

    def _event_handler(self):
        """事件"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False
            elif e.type == pygame.USEREVENT:
                enemy = Enemy_Airplane()
                self.Enemy_Airplane_group.add(enemy)
            elif e.type == pygame.KEYDOWN:
                if e.key == pygame.K_LEFT:
                    self.plane.rect.x -= 5
                    if self.plane.rect.x < 0:
                        self.plane.rect.x = 5
                elif e.key == pygame.K_RIGHT:
                    self.plane.rect.x += 5
                    if self.plane.rect.x + self.plane.rect.w>SCREEN_SIZE[0]:
                        self.plane.rect.x = SCREEN_SIZE[0]-\
                                            self.plane.rect.w-5

                if e.key == pygame.K_SPACE:
                    self.plane.fire()


plane = PlaneGame()
plane.start_game()


